﻿using EnterpriseWebsiteEntity;
using EnterpriseWebsiteUtil.Helper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace EnterpriseWebsiteBLL
{
    public class CarouselService
    {
        public readonly SqlSugarClient _db;

        public CarouselService(SqlSugarHelper sqlSugarHelper)
        {
            _db = sqlSugarHelper.sugarClient;
        }

        public bool AddCarousel(CarouselEntity entity)
        {
            //查出当前最后的排序
            var last = _db.Queryable<CarouselEntity>()
                .OrderByDescending(c => c.Sort)
                .First();
            entity.Sort = last != null ? (last.Sort + 1) : 0;
            entity.CreateDate = DateTime.Now;
            return _db.Insertable(entity).ExecuteCommand() > 0;
        }

        public CarouselEntity GetCarouselById(int id)
        {
            return _db.Queryable<CarouselEntity>().InSingle(id);
        }

        public List<CarouselEntity> GetAllCarousels()
        {
            return _db.Queryable<CarouselEntity>().ToList();
        }

        public async Task<List<CarouselEntity>> GetAllCarouselsAsync()
        {
            return await _db.Queryable<CarouselEntity>().ToListAsync();
        }



        public bool UpdateCarousel(CarouselEntity entity)
        {
            return _db.Updateable(entity).ExecuteCommand() > 0;
        }

        public bool DeleteCarousel(int id)
        {
            return _db.Deleteable<CarouselEntity>().In(id).ExecuteCommand() > 0;
        }
    }
}
